The least common multiple (LCM) of two numbers is the smallest number that is a multiple of both.
def find_lcm(x, y):
# Find the maximum of the two numbers
max_num = max(x, y)
lcm = max_num
while True:
if lcm % x == 0 and lcm % y == 0:
return lcm
lcm += max_num # Increment by the maximum number to check for the next multiple
def find_and_display_lcm():
x = int(input("Enter the first number: "))
y = int(input("Enter the second number: "))
lcm = find_lcm(x, y)
print("LCM of", x, "and", y, "is:", lcm)
# Example 1
find_and_display_lcm()
Enter the first number: 12
Enter the second number: 18
LCM of 12 and 18 is: 36
Enter the first number: 8
Enter the second number: 15
LCM of 8 and 15 is: 120
The function find_lcm(x, y)
calculates the least common multiple (LCM) of two numbers by iterating through multiples of the larger number until finding a number that is divisible by both.
The function find_and_display_lcm()
takes input for two numbers, calculates their LCM using the first function, and displays the result.